﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminNewEventController : Controller
    {
        //
        // GET: /AdminCP/AdminNewEvent/
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityNewEvents.OrderByDescending(p => p.DateCreate).ToList();
                if (query != null)
                    return View(query);
                else
                    return View("Error");
            }

        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query =from p in db.EntityNewEvents
                           where p.Id==id
                           select new AdminNewEventModel
                           {
                               Id=p.Id,
                               Name=p.Name,
                               Description=p.Description,
                               Body=p.Body,
                               DateCreate=p.DateCreate,
                               Show=p.Show,
                               Meta_keyword=p.Meta_keyword,
                               Meta_description=p.Meta_description,
                               Titletag=p.Titletag
                           };
                if (query != null)
                    return View(query.FirstOrDefault());
                else
                    return View("Error");

            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminNewEventModel model)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityNewEvents.FirstOrDefault(p => p.Id == model.Id);
                if (query != null)
                {
                    query.Name = model.Name;
                    query.Description = model.Description;
                    query.Body = model.Body;
                    if (model.DateCreate == null)
                        query.DateCreate = DateTime.Now;
                    else
                        query.DateCreate = model.DateCreate;
                    query.Show = model.Show;
                    query.Meta_keyword = model.Meta_keyword;
                    query.Meta_description = model.Meta_description;
                    query.Titletag = model.Titletag;
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                    return View("Error");
            }
        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminNewEventModel model)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityNewEvent NewEvent = new EntityNewEvent();
                NewEvent.Name = model.Name;
                NewEvent.Description = model.Description;
                NewEvent.Body = model.Body;
                if (model.DateCreate == null)
                    NewEvent.DateCreate = DateTime.Now;
                else
                    NewEvent.DateCreate = model.DateCreate;
                NewEvent.Show = model.Show;
                NewEvent.Meta_keyword = model.Meta_keyword;
                NewEvent.Meta_description = model.Meta_description;
                NewEvent.Titletag = model.Titletag;
                db.EntityNewEvents.InsertOnSubmit(NewEvent);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            
        }
        #endregion

        #region "Delete"
        public string Delete(int id)
        {
            using(dbVietPremierDataContext db=new dbVietPremierDataContext())
            {
                var imageNewEvent = db.EntityImageNewEvents.Where(p => p.IdNewEvent == id).ToList();
                db.EntityImageNewEvents.DeleteAllOnSubmit(imageNewEvent);
                EntityNewEvent NewEvent = db.EntityNewEvents.FirstOrDefault(p => p.Id == id);
                if (NewEvent != null)
                {
                    db.EntityNewEvents.DeleteOnSubmit(NewEvent);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";

            }
        }
        #endregion
    }
}
